// pages/teacher/teacher.js
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    teacherInfo:[],
    passwordAgain:"",
    password:""
  },
  goToChat1:function (params) {
    
    //获取评教率
    var url = app.globalData.url;
    var that = this;
    var courseName = params.currentTarget.dataset.coursename;
    wx.request({
      url: url+'/getEvaluationRateByCourseName', 
      data: {
        courseName:courseName
      },
      header: {
        'content-type': 'application/json' // 默认值
      },
      success (res) {
        console.log("评测率："+res.data.value)
        var evaluationRate = {
          name:"评测率",
          value:res.data.value
        }
        getApp().globalData.evaluationRate = evaluationRate;

        wx.navigateTo({
          url: '/pages/bar/chart2'
        })

      }
     })

    

   
  },
  goToChat2:function (params) {
    //获取优秀良好中等人数分布
    var url = app.globalData.url;
    var that = this;
    var courseName = params.currentTarget.dataset.coursename;
    wx.request({
      url: url+'/getGradeDistribution', 
      data: {
        courseName:courseName
      },
      header: {
        'content-type': 'application/json' // 默认值
      },
      success (res) {
        console.log("等地分布1"+JSON.stringify(res.data))
    
        getApp().globalData.gradeDistribution = res.data;

        wx.navigateTo({
          url: '/pages/bar/chart1',
        })
      }
     })


    
  },
  goToChat3:function (params) {
    var url = app.globalData.url;
    var that = this;
    var courseName = params.currentTarget.dataset.coursename;
    wx.request({
      url: url+'/getEvaluateRangeStudentNumber', 
      data: {
        courseName:courseName
      },
      header: {
        'content-type': 'application/json' // 默认值
      },
      success (res) {
        console.log("获评分数区间的学生人数："+JSON.stringify(res.data))

        getApp().globalData.evaluateRangeStudentNumber = res.data;

        wx.navigateTo({
          url: '/pages/bar/chart3',
        })
      }
     })
  },
  getExcel:function (params) {
    var url = app.globalData.url;
    var that = this;
    var courseName = params.currentTarget.dataset.coursename;

    wx.downloadFile({
      url:url+'/getQuestionAdviceByTeacherCourse?courseName='+courseName,
      // header:{
      // },
      success: function (res) {
        const filePath = res.tempFilePath
        //反馈
        wx.showToast({
          title: "下载成功",
          icon: 'success',
          duration: 1000
        })

        //打开文档
        wx.openDocument({
          filePath: filePath,
          showMenu:true,
          success: function (res) {
            console.log('打开文档成功')
          }
        })

      }
    }) 


  },

  showModal(e) {
    this.setData({
      modalName: e.currentTarget.dataset.target
    })
  },
  hideModal(e) {
    this.setData({
      modalName: null
    })
  },
  inputPassword:function (params) {
    var password = params.detail.value;
    this.setData({
      password:password
    })
  },
  inputPasswordAgain:function (params) {
    var passwordAgain = params.detail.value;
    this.setData({
      passwordAgain:passwordAgain
    })
  },

  //修改老师密码
  updateTeacherPassword:function (params) {
    this.setData({
      modalName: null
    });
    console.log("password:"+this.data.password)
    console.log("passwordAgain:"+this.data.passwordAgain)
    if(this.data.password!=this.data.passwordAgain){
      wx.showModal({
        title: '提示',
        content: '两次输入的密码不一致',
        showCancel:false
      })
    }else{
      //进入修改逻辑
      var password = this.data.password;
      var teacherId = app.globalData.teacherId;
      var url = app.globalData.url;
      var that = this;
      wx.request({
        url: url+'/updateTeacherPassword', 
        data: {
          trueTeacherId: teacherId,
          teacherPassword:password,
        },
        header: {
          'content-type': 'application/json' // 默认值
        },
        success (res) {
          console.log(res.data)
          if(res.data.value=="success"){
            wx.showToast({
              title: '密码修改成功!',
              icon: 'success',
              mask: true
            });
            that.setData({
              inputValue:""
            })
  
             //更新老师信息
             wx.request({
              url: url+'/getTrueTeacherAllInfo', 
              data: {
                trueTeacherId: teacherId
              },
              header: {
                'content-type': 'application/json' // 默认值
              },
              success (res) {
                that.setData({
                  teacherInfo:res.data
                })
              }
            })


          }else{
            wx.showModal({
              title: '警告',
              content: '服务器错误',
              showCancel:false
            })
          }
        }
      })

    }
  },
  unlogin:function (params) {
    getApp().globalData.studentId = "";
    getApp().globalData.teacherId = "";
    wx.redirectTo({
      url: '../login/login'
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var url = app.globalData.url;
    var trueTeacherId = app.globalData.teacherId;
    var that = this;
    wx.request({
      url: url+'/getTrueTeacherAllInfo', 
      data: {
        trueTeacherId: trueTeacherId
      },
      header: {
        'content-type': 'application/json' // 默认值
      },
      success (res) {
        console.log("老师的全部信息："+JSON.stringify(res.data))
        that.setData({
          teacherInfo:res.data
        })
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})